package com.scs.application.modules.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.modules.base.entity.HospitalThirdPlatform;
import com.scs.application.modules.base.mapper.HospitalThirdPlatformMapper;
import com.scs.application.modules.base.request.HospitalThirdPlatformQueryRequest;
import com.scs.application.modules.base.service.HospitalThirdPlatformService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 第三方配置 service 实现
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class HospitalThirdPlatformServiceImpl extends BaseServiceImpl<HospitalThirdPlatformMapper, HospitalThirdPlatform> implements HospitalThirdPlatformService {
    @Override
    public List<HospitalThirdPlatform> list(HospitalThirdPlatformQueryRequest request) {
        List<HospitalThirdPlatform> list = this.list(getQueryWrapper(request));
        return list;
    }

    @Override
    public IPage<HospitalThirdPlatform> page(HospitalThirdPlatformQueryRequest request) {
        IPage page = this.page(request.getPage(), getQueryWrapper(request));
        return page;
    }

    private QueryWrapper<HospitalThirdPlatform> getQueryWrapper(HospitalThirdPlatformQueryRequest request) {
        return Wrappers.<HospitalThirdPlatform>query()
                .eq(request.getOrderPlatForm() != null, "order_platform", request.getOrderPlatForm())
                .like(StringUtils.isNotEmpty(request.getHospitalName()), "hospital_name", request.getHospitalName())
                .orderByDesc("gmt_modified");
    }

    @Override
    public HospitalThirdPlatform getByHospitalId(String hospitalId){
        HospitalThirdPlatform entity = this.getOne(Wrappers.<HospitalThirdPlatform>query().eq("hospital_id",hospitalId));
        if(entity == null){
            return null;
        }
        return entity;
    }
}
